
namespace core {
    export class ViewManager extends BaseClass {
        private _views: any;
        private _opens: Array<number>;

        public constructor() {
            super();
            this._views = {};
            this._opens = new Array<number>();
        }

        //注册View
        public register(key: number, view: IBaseView): void {
            if (view == null) {
                return;
            }
            if (this._views[key]) {
                console.debug("ViewManager register key重复" + key + view)
                return;
            }
            this._views[key] = view;
        }

        //注销View
        public unregister(key: number): void {
            if (!this._views[key]) {
                return;
            }
            this._views[key] = null;
            delete this._views[key];
        }

        //获取View
        public getView(key: number): IBaseView {
            return this._views[key];
        }
        public isShow(key: number): boolean {
            return this._opens.indexOf(key) != -1;
        }

        //销毁(或替换)View
        public destroy(key: number, newView: IBaseView = null): void {
            var oldView: IBaseView = this.getView(key);
            if (oldView) {
                this.unregister(key);
                oldView.destroy();
                oldView = null;
            }
            this.register(key, newView);
        }

        public open(key: number, ...param: any[]): IBaseView {
            var view: IBaseView = this.getView(key);
            if (view == null) {
                console.error("UI_" + key + "不存在");
                return;
            }

            if (view.isShow()) {
                view.open.apply(view, param);
                return view;
            }

            if (view.isInit()) {
                view.addToParent();
                view.open.apply(view, param);
            } else {
                App.EasyLoading.showLoading()
                view.loadResource(
                    function () {
                        view.setVisible(false);
                        view.addToParent();
                    }.bind(this),
                    function () {
                        view.initUI();
                        view.initData();
                        view.open.apply(view, param);
                        view.setVisible(true);
                        App.EasyLoading.hideLoading();
                    }.bind(this))
            }

            this._opens.push(key);
            return view;
        }

        public close(key: number, ...param: any[]): void {
            if (!this.isShow(key)) {
                return;
            }
            var view: IBaseView = this.getView(key);
            if (view == null) {
                return;
            }

            var viewIndex = this._opens.indexOf(key);
            if (key >= 0) {
                this._opens.splice(viewIndex, 1);
            }

            view.removeFromParent();
            view.close.apply(view, param);
        }

        public closeView(view: IBaseView, ...param: any[]): void {
            var keys = Object.keys(this._views);
            for (var i: number = 0, len = keys.length; i < len; i++) {
                var key: number = parseInt(keys[i]);
                if (this._views[key] == view) {
                    this.close(key, param);
                    return;
                }
            }
        }

        public closeAll(): void {
            while (this._opens.length) {
                this.close(this._opens[0]);
            }
        }
    }
}